package controllers;


import beans.Contact;
import beans.Organ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import services.impl.ContactServicesImpl;
import services.impl.OrganServicesImpl;
import services.impl.ProfileServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/message")
public class OrganController {

    @Autowired
    private OrganServicesImpl osi;

    @Autowired
    private ContactServicesImpl csi;
    //1.机构信息管理
    @RequestMapping("/message")
    public List message() {
        List<Organ> list = osi.selectList();
        return list;
    }

    //2.创建信息
    @CrossOrigin(origins = "*")
    @RequestMapping("/createmessage")
    public String createmessage(@RequestBody Organ organ){
        osi.insertSelective(organ);
        return null;
    }

    //3.编辑信息
    @CrossOrigin(origins = "*")
    @RequestMapping("/editmessage")
    public String editmessage(@RequestBody Organ organ){
        osi.updateByPrimaryKeySelective(organ);
        return null;
    }

    //4.信息详情
    @RequestMapping("/detailsmessage")
    public Organ detailsmessage(HttpServletRequest request){
        int i = Integer.parseInt(request.getParameter("organId"));
        Organ organ = osi.selectByPrimaryKey(i);
        return organ;
    }

    //5.搜索信息
    @RequestMapping("/searchmessage")
    public List<Organ> searchmessage(@RequestBody Map<String,String> request){
        String select = request.get("selectText");
        String input = request.get("inputText");
        List<Organ> list = osi.selectSelective(select,input);
        return list;
    }

    //5.1.删除信息
    @RequestMapping("/deletemessage")
    public String deletemessage(HttpServletRequest request){
        int i = Integer.parseInt(request.getParameter("organId"));
        osi.deleteByPrimaryKey(i);
        return null;
    }

    //6.联系人列表
    @RequestMapping("/contact")
    public List<Contact> contact(){
        List<Contact> list = csi.selectList();
        return list;
    }

    //7.创建联系人
    @CrossOrigin(origins = "*")
    @RequestMapping("/createcontact")
    public String createcontact(@RequestBody Contact contact){
        csi.insertSelective(contact);
        return null;
    }

    //8.编辑联络
    @CrossOrigin(origins = "*")
    @RequestMapping("/editcontact")
    public String editcontact(@RequestBody Contact contact){
        csi.updateByPrimaryKeySelective(contact);
        return null;
    }

    //9.联系人详情
    @RequestMapping("/detailscontact")
    public Contact detailscontact(HttpServletRequest request){
        int i = Integer.parseInt(request.getParameter("contactId"));
        Contact contact = csi.selectByPrimaryKey(i);
        return contact;
    }

    //10.搜索联系人
    @RequestMapping("/searchcontact")
    public List<Contact> searchcontact(@RequestBody Map<String,String> request){
        String select = request.get("selectText");
        String input = request.get("inputText");
        List<Contact> list = csi.selectSelective(select,input);
        return list;
    }

    //11.删除联系人
    @RequestMapping("/deletecontact")
    public String deletecontact(HttpServletRequest request){
        int i = Integer.parseInt(request.getParameter("contactId"));
        csi.deleteByPrimaryKey(i);
        return null;
    }

}
